package czh.cocoa.ui.impl;

import czh.cocoa.foundation.NSArray;
import czh.cocoa.foundation.NSMutableArray;
import czh.cocoa.foundation.NSObject;
import czh.cocoa.foundation.NSSelector;
import czh.cocoa.foundation.NSString;
import czh.cocoa.graphics.CGRect;
import czh.cocoa.graphics.CGSize;
import czh.cocoa.ui.UIAnimationBlock;
import czh.cocoa.ui.UIApplication;
import czh.cocoa.ui.UIColor;
import czh.cocoa.ui.UIFont;
import czh.cocoa.ui.UIImage;
import czh.cocoa.ui.UIView;
import czh.cocoa.ui.UIWindow;

public class UIAlertView extends UIView {

	private NSString mTitle;
	
	private NSString mMessage;
	
	private UIAlertViewDelegate mDelegate;
	
	private NSMutableArray mButtonTitles;
	
	public UIAlertView() {
		
	}
	
	public UIAlertView(NSString title, NSString message, UIAlertViewDelegate delegate, 
			NSString cancelButtonTitle, NSString... otherButtonTitles) {
		mTitle = title;
		mMessage = message;
		mDelegate = delegate;
		
		mButtonTitles.addObject(cancelButtonTitle);
		for (NSString btn_title : otherButtonTitles) {
			mButtonTitles.addObject(btn_title);
		}
		 
	}
	
	protected void init() {
		super.init();
		mButtonTitles = new NSMutableArray();
	}
	
	public void setTitle(NSString title) {
		mTitle = title;
	}
	
	public NSString title() {
		return mTitle;
	}
	
	public void setMessage(NSString message) {
		mMessage = message;
	}
	
	public NSString message() {
		return mMessage;
	}
	
	public int addButtonWithTitle(NSString title) {
		mButtonTitles.addObject(title);
		return mButtonTitles.count() - 1;
	}
	
	public NSString buttonTitleAtIndex(int buttonIndex) {
		return (NSString) mButtonTitles.objectAtIndex(buttonIndex);
	}
	
	public void setDelegate(UIAlertViewDelegate delegate) {
		mDelegate = delegate;
	}
	
	public UIAlertViewDelegate delegate() {
		return mDelegate;
	}
	
	public void show() {
		clearAllSubviews();
		
		UIWindow window = UIApplication.sharedApplication().keyWindow();
		CGRect bounds = window.bounds();
		setFrame(bounds);
		
		UIImageView mask_view = new UIImageView(bounds);
		mask_view.setContentMode(UIViewContentModeCenter); 
		mask_view.setImage(UIImage.imageNamed(NSTR("cocoa/dialog_mask")));  
		addSubview(mask_view);
		
		UIImage background = UIImage.imageNamed(NSTR("cocoa/dialog_background")); 
		final UIImageView bgview = new UIImageView(background);
		bgview.setContentStretch(CGRectMake(0, 0.6f, 1, 0.1f)); 
		bgview.setUserInteractionEnabled(true);
		addSubview(bgview);
		
		float bg_width = bounds.size.width * 0.88f;
		float bg_height = 0; 
		float bg_min_height = DIP(background.size().height);
		float bg_max_height = bounds.size.height * 0.88f;
		
		float padding = 10;
		
		bg_height = padding * 3;
		
		if (mTitle != null) { 
			UILabel title_label = new UILabel(); 
			title_label.setText(mTitle); 
			title_label.setFont(UIFont.boldSystemFontOfSize(25)); 
			title_label.setTextColor(UIColor.whiteColor());
			CGSize title_size = CGSizeMake(bg_width - padding * 2, bg_max_height - bg_height - padding);
			title_size = title_label.sizeThatFits(title_size);
			title_label.setFrame(CGRectMake((bg_width - title_size.width) / 2, bg_height, 
					title_size.width, title_size.height)); 
			bgview.addSubview(title_label);
						
			bg_height += title_size.height + padding;
		}
		
		if (mMessage != null) { 
			UILabel msg_label = new UILabel(); 
			msg_label.setText(mMessage); 
			msg_label.setFont(UIFont.systemFontOfSize(23)); 
			msg_label.setTextColor(UIColor.whiteColor());
			msg_label.setNumberOfLines(-1);
			CGSize msg_size = CGSizeMake(bg_width - padding * 2, bg_max_height - bg_height - padding);
			msg_size = msg_label.sizeThatFits(msg_size);
			msg_label.setFrame(CGRectMake((bg_width - msg_size.width) / 2, bg_height, 
					msg_size.width, msg_size.height)); 
			bgview.addSubview(msg_label);
			
			bg_height += msg_size.height + padding;
		}
		
		NSArray btn_titles = mButtonTitles;
		int btn_count = btn_titles.count();
        if (btn_count > 0) {
			UIImage btn_bg = UIImage.imageNamed(NSTR("cocoa/dialog_button"));
			UIImage btn_bg2 = UIImage.imageNamed(NSTR("cocoa/dialog_button_highlighted"));
			float btn_height = DIP(btn_bg.size().height);
			
			if (btn_count == 2) {
				
				float btn_width = (bg_width - padding * 2 * (btn_count + 1)) / btn_count;
				float offx = padding * 2;
				 
				for (int i = 0; i < btn_count; i++) {
					NSString btn_title = (NSString)btn_titles.objectAtIndex(i);
					UIButton button = new UIButton();
					button.setContentStretch(CGRectMake(0.5f, 0, 0.05f, 1)); 
					button.setTitleForState(btn_title, UIControl.UIControlStateNormal); 
					button.setTitleColorForState(UIColor.whiteColor(), UIControl.UIControlStateNormal);
					button.setBackgroundImageForState(btn_bg, UIControl.UIControlStateNormal);
					button.setBackgroundImageForState(btn_bg2, UIControl.UIControlStateHighlighted);
					button.titleLabel().setFont(UIFont.boldSystemFontOfSize(25));
					button.setFrame(CGRectMake(offx, bg_height, btn_width, btn_height)); 
					
					final int index = i;
					button.addTargetActionForControlEvents(this, new NSSelector() {
						@Override
						public void perform(NSObject receiver, NSObject params) {
							dismissWithClickedButtonIndexAnimated(index, true);
						}
					}, UIControl.UIControlEventTouchUpInside);
					
					bgview.addSubview(button);
				
					offx += btn_width + padding * 2;
				}
				
				bg_height += btn_height + padding;
				
			} else {
				
				for (int i = 0; i < btn_count; i++) {
					NSString btn_title = (NSString)btn_titles.objectAtIndex(i);
					UIButton button = new UIButton();
					button.setContentStretch(CGRectMake(0.5f, 0, 0.05f, 1)); 
					button.setTitleForState(btn_title, UIControl.UIControlStateNormal); 
					button.setTitleColorForState(UIColor.whiteColor(), UIControl.UIControlStateNormal);
					button.setBackgroundImageForState(btn_bg, UIControl.UIControlStateNormal);
					button.setBackgroundImageForState(btn_bg2, UIControl.UIControlStateHighlighted);
					
					button.titleLabel().setFont(UIFont.boldSystemFontOfSize(25));
					
					button.setFrame(CGRectMake(padding * 2, bg_height, 
							        bg_width - padding * 4, btn_height)); 
					
					final int index = i;
					button.addTargetActionForControlEvents(this, new NSSelector() {
						@Override
						public void perform(NSObject receiver, NSObject params) {
							dismissWithClickedButtonIndexAnimated(index, true);
						}
					}, UIControl.UIControlEventTouchUpInside);
					
					bgview.addSubview(button);
					
					bg_height += btn_height + padding;
				}
				
			}
			
			bg_height += padding * 2;
				
		}
		
		if(bg_height < bg_min_height) bg_height = bg_min_height;
		
		final CGRect bg_frame = CGRectMake((bounds.size.width - bg_width) / 2, 
				(bounds.size.height - bg_height) / 2, bg_width, bg_height); 
		bgview.setFrame(bg_frame);
		
//		CGRect bg_frame0 = new CGRect(bg_frame);
//		bg_frame0.origin.y = bounds.size.height;
//		bgview.setFrame(bg_frame0);
//		
//		UIView.animateWithDurationAnimations(0.2, new UIAnimationBlock() {
//			@Override
//			public void run() {
//				bgview.setFrame(bg_frame);
//			}
//		});
		
		window.addSubview(this);
		
		if (mDelegate != null) {
			mDelegate.didPresentAlertView(this);
		}
	}
	
	public void dismissWithClickedButtonIndexAnimated(int buttonIndex, boolean animted) {
		if (mDelegate != null) {
			mDelegate.alertViewClickButtonAtIndex(this, buttonIndex);
		}
		
		removeFromSuperview();
		
		if (mDelegate != null) {
			mDelegate.alertViewDidDismissWithButtonIndex(this, buttonIndex);
		}
	}
	
}
